package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.DTO.WapTransferBO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.UserPoints;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.PointsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/points")
public class PointsController extends BaseController {
    @Autowired
    private PointsService pointsService;

    /**
     * 查询总积分
     */
    @GetMapping("/getTotalPoints/{userId}")
    public AjaxResult saveUserPrizeRecord(@PathVariable Long userId) {
        UserPoints userPoints = pointsService.selectUserPointsByUserId(userId);
        if(ObjectUtils.isEmpty(userPoints)){
            return success(0);
        }
        return success(userPoints);
    }

    /**
     * 查询积分变更记录
     */
    @GetMapping("/getUserPointsLog/{userId}")
    public TableDataInfo getUserPointsLog(@PathVariable Long userId) {
        return getDataTable(pointsService.selectUserPointsLogByUserId(userId));
    }

    /**
     * 管理员给指定用户开通会员
     */
    @GetMapping("/openMember/{userId}")
    public AjaxResult openMember(@PathVariable Long userId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()){
            return error("当前账号权限不足，请联系管理员操作！");
        }
        pointsService.updateLevel(userId);
        return success();
    }

}
